//! World builder used to set up world state.

use std::collections::*;
use std::sync::Arc;

use craftdig_math::ChunkCoord;

use super::{ChunkState, WorldState};
use crate::entity::{EntityId, EntityState};

pub struct WorldBuilder {
    chunks: HashMap<ChunkCoord, ChunkState>,
    entities: BTreeMap<EntityId, EntityState>,
}

impl WorldBuilder {
    pub fn new() -> Self {
        Self {
            chunks: HashMap::new(),
            entities: BTreeMap::new(),
        }
    }

    pub fn finish(self) -> WorldState {
        let chunks = self
            .chunks
            .into_iter()
            .map(|(cc, cs)| (cc, Arc::new(cs)))
            .collect::<_>();
        let entities = self
            .entities
            .into_iter()
            .map(|(id, es)| (id, Arc::new(es)))
            .collect::<_>();
        WorldState::new(chunks, entities)
    }
}
